package net.zoneland.zrdp.common.utils.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import javax.servlet.ServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 通用http工具封装
 *
 * @author zonevue
 */
public class HttpHelper {
    private static final Logger LOGGER = LoggerFactory.getLogger(HttpHelper.class);
    private HttpHelper() {
        throw new IllegalStateException("Utility class");
    }
    public static String getBodyString(final ServletRequest request) {
        final StringBuilder sb = new StringBuilder();
        try (final InputStream inputStream = request.getInputStream();
             final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {

            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (final IOException e) {
            LOGGER.error("Error reading request body", e);
        }
        return sb.toString();
    }
}
